package st.business.tepth.business.user.busnumber.workholder.maintain.work.content;

import java.util.ArrayList;
import java.util.List;

import st.business.tepth.base.OnDataConvertListener;
import st.business.tepth.business.bean.MaintainNotCompleteBean;
import st.business.tepth.business.bean.PickGoods;
import st.business.tepth.business.bean.RepairTaskBean;
import st.business.tepth.business.bean.RepairTaskTypeBean;

/**
 * Author: shadow
 * Email:  1025431817@qq.com
 * Time:   2019-03-23 09:46
 */

public class MaintainWorkPresenter extends MaintainWorkContract.Presenter {
    private final MaintainWorkContract.View mView;
    private final MaintainWorkContract.Model mModel;

    public MaintainWorkPresenter(MaintainWorkContract.View view) {
        mView = view;
        mModel = new MaintainWorkModel();
    }

    @Override
    void getWorkData(String token, int repairId, int vehicleSeriesId) {
        addDisposable(mModel.getWorkData(token, repairId,
                vehicleSeriesId,
                new OnDataConvertListener<RepairTaskTypeBean>() {
                    @Override
                    public void convert(RepairTaskTypeBean workBean) {
                        ArrayList<RepairTaskBean> repairTaskList = workBean.getRepairTaskList();
                        if (repairTaskList != null && !repairTaskList.isEmpty()) {
                            mView.showRepairTask(repairTaskList.get(0));
                        } else {
                            mView.showRepairTask(null);
                        }

                    }

                    @Override
                    public void error(int code, String msg) {
                        mView.showError(msg);
                    }
                }));
    }

    @Override
    void commitProgram(String token, int repairId, int repairTaskId, int vehicleSeriesId,
                       String taskItemList, String maintenanceOperationList) {
        addDisposable(mModel.commitProgram(token, repairId, repairTaskId, vehicleSeriesId,
                taskItemList, maintenanceOperationList, new OnDataConvertListener<List<PickGoods>>() {
                    @Override
                    public void convert(List<PickGoods> programConfirmPickGoods) {
                        mView.showPickGoods(programConfirmPickGoods);
                    }

                    @Override
                    public void error(int code, String msg) {
                        mView.showError(msg);
                    }
                }, new OnDataConvertListener<RepairTaskTypeBean>() {
                    @Override
                    public void convert(RepairTaskTypeBean repairTaskTypeBean) {
                        ArrayList<RepairTaskBean> repairTaskList = repairTaskTypeBean.getRepairTaskList();
                        if (repairTaskList != null && !repairTaskList.isEmpty()) {
                            mView.showRepairTask(repairTaskList.get(0));
                        } else {
                            mView.showRepairTask(null);
                        }
                    }

                    @Override
                    public void error(int code, String msg) {
                        mView.showError(msg);
                    }
                }));
    }

    @Override
    void submitFinishWork(String token, int repairId, int repairTaskId, int vehicleSeriesId) {
        addDisposable(mModel.submitFinishWork(token, repairId, repairTaskId,
                vehicleSeriesId,
                new OnDataConvertListener<RepairTaskTypeBean>() {
                    @Override
                    public void convert(RepairTaskTypeBean workBean) {
                        ArrayList<RepairTaskBean> repairTaskList = workBean.getRepairTaskList();
                        if (repairTaskList != null && !repairTaskList.isEmpty()) {
                            mView.showRepairTask(repairTaskList.get(0));
                        } else {
                            mView.showRepairTask(null);
                        }
                        mView.finishSucceed();
                    }

                    @Override
                    public void error(int code, String msg) {
                        mView.showError(msg);
                    }
                }, new OnDataConvertListener<MaintainNotCompleteBean>() {
                    @Override
                    public void convert(MaintainNotCompleteBean maintainNotCompleteBean) {
                        mView.showNotWorkMaintain(new MaintainWorkDataConvert()
                                .convert(maintainNotCompleteBean));
                    }

                    @Override
                    public void error(int code, String msg) {

                    }
                },
                new OnDataConvertListener<String>() {
                    @Override
                    public void convert(String s) {
                        mView.submitFinishSucceed();
                    }

                    @Override
                    public void error(int code, String msg) {
                        mView.showError(msg);
                    }
                }));
    }

    @Override
    void pickGoods(String token, int repairId, int vehicleSeriesId,
                   String materialApplyList) {
        addDisposable(mModel.pickGoods(token, repairId,
                vehicleSeriesId, materialApplyList,
                new OnDataConvertListener<String>() {
                    @Override
                    public void convert(String s) {
                        mView.pickGoodsSucceed();
                    }

                    @Override
                    public void error(int code, String msg) {
                        mView.showError(msg);
                    }
                }, new OnDataConvertListener<RepairTaskTypeBean>() {

                    @Override
                    public void convert(RepairTaskTypeBean workBean) {
                        ArrayList<RepairTaskBean> repairTaskList =
                                workBean.getRepairTaskList();
                        if (repairTaskList != null && !repairTaskList.isEmpty()) {
                            mView.showRepairTask(repairTaskList.get(0));
                        } else {
                            mView.showRepairTask(null);
                        }
                    }

                    @Override
                    public void error(int code, String msg) {
                        mView.showError(msg);
                    }
                }));
    }

    @Override
    void commitNotWork(String token, int repairId, String workList) {
        addDisposable(mModel.commitNotWork(token, repairId, workList, new OnDataConvertListener<String>() {
            @Override
            public void convert(String s) {
                mView.commitNotWorkSucceed();
            }

            @Override
            public void error(int code, String msg) {
                mView.showError(msg);
            }
        }));
    }

    @Override
    void cancelFinishWork(String token, int repairId, int repairTaskId, int vehicleSeriesId) {
        addDisposable(mModel.cancelFinishWork(token, repairId, repairTaskId,
                vehicleSeriesId,
                new OnDataConvertListener<String>() {
                    @Override
                    public void convert(String s) {
                        if (mView != null) {
                            mView.cancelFinishWorkSucceed();
                        }
                    }

                    @Override
                    public void error(int code, String msg) {
                        if (mView != null) {
                            mView.showError(msg);
                        }
                    }
                }, new OnDataConvertListener<RepairTaskTypeBean>() {
                    @Override
                    public void convert(RepairTaskTypeBean repairTaskTypeBean) {
                        ArrayList<RepairTaskBean> repairTaskList = repairTaskTypeBean.getRepairTaskList();
                        if (repairTaskList != null && !repairTaskList.isEmpty()) {
                            mView.showRepairTask(repairTaskList.get(0));
                        } else {
                            mView.showRepairTask(null);
                        }
                    }

                    @Override
                    public void error(int code, String msg) {
                        if (mView != null) {
                            mView.showError(msg);
                        }
                    }
                }));
    }
}
